"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix_yc = "YCD" + formatDate(new Date(), "yyyyMMdd");
var str_prefix_hy = "HYD" + formatDate(new Date(), "yyyyMMdd");

describe("approval", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
    });

    after(function() {
        clearDB();
    });

    it("创建车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "苏A123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "2.0",
            "year": "2008",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("创建用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);

        // 其他用户的审批不在列表中展示
        result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 1,
            "start_date_time": "2099-12-15 13:00",
            "end_date_time": "2099-12-15 14:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 3);
    });

    it("审批列表", function() {
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.list[1].id, 1);
        assert.equal(result.data.list[1].status, "待处理");
        assert.equal(result.data.list[1].vehicleRecord_no, str_prefix_yc + "001");
        assert.equal(result.data.list[1].vehicleRecord_status, "待审核");
    });

    it("通过用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
    });

    it("审批列表", function() {
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].status, "待处理");

        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "已处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].status, "已处理");
        assert.equal(result.data.list[0].vehicleRecord_no, str_prefix_yc + "001");
        assert.equal(result.data.list[0].vehicleRecord_status, "已通过");
    });

    it("审批统计", function() {
        var result = httpPost("POST", "/v1.0/api/approval/Static", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 2);
        assert.equal(result.data.unreviewed, 1);
        assert.equal(result.data.reviewed, 1);
    });

    it("撤回用车单", function() {
        var result = httpPost("POST", "/v1.0/api/vehicleRecord/Withdraw", {
            "id": 1,
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Withdraw", {
            "id": 2,
        })
        assert.equal(result.code, "200");
    });

    it("根据 bindtype 和 bindid 查询审批", function() {
        // 未处理审批因撤回删除
        var result = httpPost("POST", "/v1.0/api/approval/Get", {
            "bind_type": "vehicle",
            "bind_id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });


    it("审批列表", function() {
        // 未处理审批因撤回删除
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);

        // 已处理审批不受撤回影响
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "已处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].status, "已处理");
    });

    // it("过期筛选", function() {
    //     // 过期用车单
    //     var result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
    //         "vehicle_id": 1,
    //         "start_date_time": "2009-12-15 10:00",
    //         "end_date_time": "2009-12-15 12:00",
    //         "use_reason": "用车事由",
    //         "destination": "目的地",
    //         "address": "目的地地址",
    //         "reviewed_by": 1,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);

    //     // 总计2张用车单
    //     result = httpPost("POST", "/v1.0/api/approval/List", {
    //         "page": 1,
    //         "limit": 10,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.list.length, 2);
    //     assert.equal(result.data.list[0].id, 4);
    //     assert.equal(result.data.list[1].id, 1);

    //     // 1张 未过期
    //     result = httpPost("POST", "/v1.0/api/approval/List", {
    //         "ignore_overdue": true,
    //         "page": 1,
    //         "limit": 10,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.list.length, 1);
    //     assert.equal(result.data.list[0].id, 1);
    // })

    it("创建会议室", function() {
        var result = httpPost("POST", "/v1.0/api/meetingroom/Create", {
            "name": "会议室101",
            "load": 20,
            "building": "中心楼",
            "floor": 1,
            "device": "白板,空调,会议桌椅",
            "remark": "隔音不好",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
    });

    it("创建会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 12:00",
            "end_date_time": "2099-12-15 13:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);

        // 其他用户的审批不在列表中展示
        result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
            "meetingroom_id": 1,
            "start_date_time": "2099-12-15 13:00",
            "end_date_time": "2099-12-15 14:00",
            "meeting_title": "例行周会",
            "meeting_content": "各抒己见",
            "user_number": 10,
            "reviewed_by": 2,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 3);
    });

    it("审批列表", function() {
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 3);
        assert.equal(result.data.list[1].id, 5);
        assert.equal(result.data.list[1].status, "待处理");
        assert.equal(result.data.list[1].meetingRecord_no, str_prefix_hy + "001");
        assert.equal(result.data.list[1].meetingRecord_status, "待审核");
    });

    it("通过会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Pass", {
            "id": 1,
        })
        assert.equal(result.code, "200");
    });

    it("审批列表", function() {
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.list[0].id, 6);
        assert.equal(result.data.list[0].bind_type, "meeting");
        assert.equal(result.data.list[0].status, "待处理");

        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "已处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.list[0].id, 5);
        assert.equal(result.data.list[0].status, "已处理");
        assert.equal(result.data.list[0].meetingRecord_no, str_prefix_hy + "001");
        assert.equal(result.data.list[0].meetingRecord_status, "已通过");
    });

    it("审批统计", function() {
        var result = httpPost("POST", "/v1.0/api/approval/Static", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 4);
        assert.equal(result.data.unreviewed, 2);
        assert.equal(result.data.reviewed, 2);
    });

    it("撤回会议预约单", function() {
        var result = httpPost("POST", "/v1.0/api/meetingRecord/Withdraw", {
            "id": 1,
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/meetingRecord/Withdraw", {
            "id": 2,
        })
        assert.equal(result.code, "200");
    });

    it("审批列表", function() {
        // 未处理审批因撤回删除
        var result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "待处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 1);

        // 已处理审批不受撤回影响
        result = httpPost("POST", "/v1.0/api/approval/List", {
            "status": "已处理",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 2);
        assert.equal(result.data.list[0].id, 5);
        assert.equal(result.data.list[0].status, "已处理");
    });

    // it("过期筛选", function() {
    //     // 过期会议预约单
    //     var result = httpPost("POST", "/v1.0/api/meetingRecord/Create", {
    //         "meetingroom_id": 1,
    //         "start_date_time": "2009-12-15 13:00",
    //         "end_date_time": "2009-12-15 14:00",
    //         "meeting_title": "例行周会",
    //         "meeting_content": "各抒己见",
    //         "user_number": 10,
    //         "reviewed_by": 1,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.id, 4);

    //     // 总计2张会议预约单,2张用车预约
    //     result = httpPost("POST", "/v1.0/api/approval/List", {
    //         "page": 1,
    //         "limit": 10,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.list.length, 4);
    //     assert.equal(result.data.list[0].id, 8);
    //     assert.equal(result.data.list[1].id, 5);

    //     // 1张 未过期
    //     result = httpPost("POST", "/v1.0/api/approval/List", {
    //         "ignore_overdue": true,
    //         "page": 1,
    //         "limit": 10,
    //     })
    //     assert.equal(result.code, "200");
    //     assert.equal(result.data.list.length, 2);
    //     assert.equal(result.data.list[0].id, 5);
    // })
});

require('test').run();